* *** Grab 100% samples for all of the years 

* foreach YEAR of num 1999/2016{
* 	disp `YEAR'
* 	cd /disk/aging/medicare/data/100pct/med
* 	use `YEAR'/1/med`YEAR'.dta, clear 

* 	keep admsndt dschrgdt drg_cd pmt_amt
* 	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct/raw/medpartemp_`YEAR'.dta, replace 
* }

* * ****************************************************************
* * * *** Prep unique grouping crosswalks

* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct/

* *** For early DRG 
* use drgcrosswalkgrouped.dta
* keep drg_cd group
* duplicates drop
* drop if mi(drg_cd)
* save drgcrosswalkgrouped_pre.dta

* *** For late DRG 
* use drgcrosswalkgrouped.dta
* keep msdrg group
* duplicates drop
* drop if msdrg == "."
* save drgcrosswalkgrouped_post.dta



* * * ***************************************************************
* * * *** Combine
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct/raw

* use medpartemp_1999.dta
* foreach YEAR of num 2000/2016{
* 	append using medpartemp_`YEAR'.dta 
* }
* save medpar_100pct.dta, replace

* **** Clean
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct/raw

* use medpar_100pct.dta, clear 

* * *** Check drg strings all the same length
* gen s = strlen(drg_cd)
* qui summarize s
* assert r(min) == r(max)
* rename drg_cd code 

* * **** Make single date variables and stay length
* rename dschrgdt date
* rename admsndt admit_date

* * *** Generate stay length
* gen staylength = date - admit_date

* * ** Drop missing data 
* drop if mi(date)|mi(code)|mi(staylength)

* *** Drop 2016 fiscal year data due to DRG change 
* disp mdy(10, 1, 2015)
* drop if date >= 20362

* *** Code for crosswalking
* disp mdy(10,1,2007)
* gen after_sample = 1 if date >= 17440
* replace after_sample = 0 if  date < 17440

* gen msdrg = code if after_sample == 1
* gen drg_cd = code if after_sample == 0 

* * *** Group all claims into group codes
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrols/100pct/

* * Split to merge before/after DRG change
* ** Early group 
* preserve 
* keep if after_sample == 0 
* merge m:1 drg_cd using drgcrosswalkgrouped_pre.dta
* drop _merge
* save medpartemp_pre.dta, replace 
* restore

* ** Later group 
* preserve 
* keep if after_sample == 1
* merge m:1 msdrg using drgcrosswalkgrouped_post.dta
* drop _merge 
* save medpartemp_post.dta, replace 
* restore

* clear 
* * *******************************************
* **** Combine into 1 file

* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/kyphon/synthcontrolsshort/100pct

* use medpartemp_pre.dta
* append using medpartemp_post.dta

* save medpar_100pct_clean.dta 
* clear 